package org.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.json.simple.JSONValue;
import org.model.User;
import org.model.Wiki;
import org.service.WikiService;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tedzy on 2015/5/18.
 * Wiki Action
 */
public class WikiAction extends ActionSupport {
    private int wikiid;
    private String title;
    private String content;
    private String reason;
    private List<Wiki> searchResult;
    private String jsonResult;
    private WikiService wikiService;
    private String redirect;

    /**
     * 建立新Wiki页面。
     * URL: /wikido/newwikipage.json 方法: POST
     * 输入: title 标题, content 内容。 需要登录
     * @return result: error 错误 success 成功。
     *          info: result为错误时有效，内容为错误信息。
     *          wikiid: result为成功时有效，内容为新建立的wiki页面的id。
     */
    public String newWikiPage(){
        Map<String, Object> json = new LinkedHashMap<>();
        User user = isLogin();
        boolean check = true;
        if(user==null || user.getId() <= 0 ){
            json.put("result", "error");
            json.put("info", "未登录。");
            check = false;
        }
        if(title == null || title.equals("")){
            json.put("result", "error");
            json.put("info", "标题不能为空。");
            check = false;
        }
        if(content == null || content.equals("")){
            json.put("result", "error");
            json.put("info", "内容不能为空。");
            check = false;
        }
        if(check){
            int wikiid = wikiService.newWiki(title, content);
            wikiService.newHistory(wikiid, user.getId(), title, content, "建立页面。");
            json.put("result", "success");
            json.put("wikiid", wikiid);
        }

        jsonResult = JSONValue.toJSONString(json);
        return SUCCESS;
    }

    /**
     * 编辑Wiki页面。
     * URL: /wikido/edit.json 方法：POST
     * 所需参数: wikiid 待编辑页面的ID， title 新标题， content 新内容， reason 编辑原因。
     * @return result: error 错误 success 成功。
     *          info: result为错误时有效，内容为错误信息。
     */
    public String editPage(){
        Map<String, Object> json = new LinkedHashMap<>();
        User user = isLogin();
        boolean check = true;
        if(user==null || user.getId() <= 0 ){
            json.put("result", "error");
            json.put("info", "未登录。");
            check = false;
        }
        if(wikiid <= 0){
            json.put("result", "error");
            json.put("info", "WIKI_ID不正确。");
            check = false;
        }
        if(title == null || title.equals("")){
            json.put("result", "error");
            json.put("info", "标题不能为空。");
            check = false;
        }
        if(content == null || content.equals("")){
            json.put("result", "error");
            json.put("info", "内容不能为空。");
            check = false;
        }
        if(reason == null || reason.equals("")){
            json.put("result", "error");
            json.put("info", "编辑原因不能为空。");
            check = false;
        }
        if(check){
            if(!wikiService.editWiki(wikiid, title, content)){
                json.put("result", "error");
                json.put("info", "存入数据库时候发生错误，可能是没有此ID对应的页面。");
            }else{
                wikiService.newHistory(wikiid, user.getId(), title, content, reason);
                json.put("result", "success");
            }
        }
        jsonResult = JSONValue.toJSONString(json);
        return SUCCESS;
    }

    /**
     * 读取wiki内容。
     * URL: /wiki/:title
     * 原始URL: /wikido/getwiki.json?title=:title 方法: GET
     * 参数 title wiki标题
     * @return jsp 页面
     */
    public String getWiki(){
        Map request = ActionContext.getContext().getParameters();
        if(request.get("redirectfrom") == null){
            redirect = null;
        }else {
            redirect = ((String[]) request.get("redirectfrom"))[0];
        }
        if(title == null || title.equals("")){
            jsonResult = "标题不见了？";
            content = "/home";
            return ERROR;
        }
        Wiki wiki = wikiService.getWiki(title);
        if(wiki == null || wiki.getId() <= 0){
            searchResult = wikiService.searchWiki(title);
            return INPUT;
        }
        wikiid = wiki.getId();
        title = wiki.getTitle();
        content = wiki.getContent();
        return SUCCESS;
    }

    private User isLogin(){
        Map<String, Object> session = ActionContext.getContext().getSession();
        Object login_status = session.get("login_status");
        if(login_status != null && (boolean) login_status){
            return (User) session.get("login_user");
        }else{
            return null;
        }
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getJsonResult() {
        return jsonResult;
    }

    public void setJsonResult(String jsonResult) {
        this.jsonResult = jsonResult;
    }

    public WikiService getWikiService() {
        return wikiService;
    }

    public void setWikiService(WikiService wikiService) {
        this.wikiService = wikiService;
    }

    public int getWikiid() {
        return wikiid;
    }

    public void setWikiid(int wikiid) {
        this.wikiid = wikiid;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public List<Wiki> getSearchResult() {
        return searchResult;
    }

    public void setSearchResult(List<Wiki> searchResult) {
        this.searchResult = searchResult;
    }

    public String getRedirect() {
        return redirect;
    }

    public void setRedirect(String redirect) {
        this.redirect = redirect;
    }
}
